import { defineConfig } from "vite";
import tailwindcss from "tailwindcss";
import autoprefixer from "autoprefixer";
import uni from "@dcloudio/vite-plugin-uni";
import postcssRemToResponsivePixel from "postcss-rem-to-responsive-pixel";
import { UnifiedViteWeappTailwindcssPlugin as uvwt } from "weapp-tailwindcss-webpack-plugin/vite";

const isH5 = process.env.UNI_PLATFORM === "h5";
const isApp = process.env.UNI_PLATFORM === "app";
const WeappTailwindcssDisabled = isH5 || isApp;

const vitePlugins = [
    uni(),
    uvwt({
        rem2rpx: true,
        disabled: WeappTailwindcssDisabled
    })
];

const postcssPlugin = [autoprefixer(), tailwindcss()];

if (!WeappTailwindcssDisabled) {
    postcssPlugin.push(
        postcssRemToResponsivePixel({
            rootValue: 32,
            propList: ["*"],
            transformUnit: "rpx",
        })
    );
}

// https://vitejs.dev/config/
export default defineConfig({
    css: {
        postcss: {
            plugins: postcssPlugin
        }
    },
    plugins: vitePlugins
});
